<template>
    <div class="login">
        <div class="login-main">
            <h1>Steam PY市场</h1>
            <div class="ms-login">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="50px">
                    <el-form-item label="账号" prop="name" class="item">
                        <el-input v-model="ruleForm.name" clearable autocomplete="off">
                            <i style="color:#0bdf6b;line-height: 50px" slot="prefix" class="el-icon-s-custom"></i>
                        </el-input>
                    </el-form-item>

                    <el-form-item label="密码" prop="pass">
                        <el-input v-model="ruleForm.pass" show-password clearable autocomplete="off">
                            <i style="color:#0bdf6b;line-height: 50px" slot="prefix" class="el-icon-lock"></i>
                        </el-input>
                    </el-form-item>

                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" show-password clearable
                                  autocomplete="off">
                            <i style="color:#0bdf6b;line-height: 50px" slot="prefix" class="el-icon-lock"></i>
                        </el-input>
                    </el-form-item>
                    <el-form-item
                            prop="email"
                            label="邮箱"
                            :rules="[
                 { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                 { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                  ]">
                        <el-input v-model="ruleForm.email" clearable>
                            <i style="color:#0bdf6b;line-height: 50px" slot="prefix" class="el-icon-message"></i>

                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')" round>进行注册</el-button>
                        <el-link type="primary" class="register" href="/">已有账号，立即登录？</el-link>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import {register} from "../api";
export default {
        name: "Register",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    name: '',
                    pass: '',
                    checkPass: '',
                    email: '',
                },
              registerForm:{
                  username: '',
                  password:'',
                  email:''
              },
                rules: {
                    name: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                        {min: 5, max: 13, message: '长度在 5 到 13 个字符', trigger: 'blur'}
                    ],
                    pass: [
                      {required: true, message: '请输入密码', trigger: 'blur'},
                      {min: 5, max: 13, message: '长度在 5 到 13 个字符', trigger: 'blur'},
                      {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                      {required: true, message: '请输入密码', trigger: 'blur'},
                      {min: 5, max: 13, message: '长度在 5 到 13 个字符', trigger: 'blur'},
                      {validator: validatePass2, trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                      this.registerForm.username=this.ruleForm.name;
                      this.registerForm.password=this.ruleForm.pass;
                      this.registerForm.email=this.ruleForm.email;
                      register(this.registerForm).then((res) =>{
                        this.$message.success("注册成功，请前往登录！")
                        this.$router.push("/")
                      })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .login {
        position: fixed;
        height: 100%;
        width: 100%;
        background-color: #cdeee8;
        text-align: center;
        /*display: inline-block;*/
    }

    .ms-login {
        padding-top: 100px;
        padding-right: 30px;
        font-min-size: 30px;
    }

    .login-main {
        position: absolute;
        left: 50%;
        width: 300px;
        height: 160px;
        margin-left: -190px;
        /*margin-top: -150px;*/
        padding: 40px;
        border-radius: 10px;
        /*background-color: darkred;*/
    }

    >>> .el-form-item__label {
        line-height: 30px;
        font-size: 17px;
        text-align: center;
    }

    >>> .el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label:before {
        content: ' ';
    }

    >>> .el-input {
        font-size: 18px;
    }

    >>> .el-input__inner {
        border-radius: 10px;
        height: 50px;
        line-height: 50px;
        font-size: 20px;

    }

    >>> .el-button.is-round {
        width: 100%;
    }
</style>
